package com.panlf.springbootdemo.dao;

import cn.hutool.core.text.StrFormatter;
import com.panlf.springbootdemo.entity.UserEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.SingleColumnRowMapper;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;

/**
 * @author panlf
 * @since 2024/1/19 16:09
 */
@Repository
public class UserDao {

    @Resource
    private JdbcTemplate jdbcTemplate;

    public void insert(UserEntity user) {
        String temp = "INSERT INTO t_user (username,password) VALUES ('{}','{}')";

        String sql = StrFormatter.format(temp, user.getUsername(), user.getPassword());

        jdbcTemplate.execute(sql);
    }


    public UserEntity findById(Integer id) {
        String temp = "select * from t_user where id = {}";

        String sql = StrFormatter.format(temp, id);

        UserEntity userDO = jdbcTemplate.queryForObject(sql, (resultSet, i) -> SingleColumnRowMapper.newInstance(UserEntity.class).mapRow(resultSet, i));

        return userDO;
    }

}
